package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.SubDepartment;
import com.woniuxy.entity.dto.GoodsManagementDTO;
import com.woniuxy.mapper.GoodsManagementMapper;
import com.woniuxy.service.GoodsManagementService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class GoodsManagementServiceImpl implements GoodsManagementService {
    @Resource
    private GoodsManagementMapper goodsManagementMapper;
    @Override
    public ResponseData<?> queryAllGoods(PageData<GoodsManagementDTO> pageData) {
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<GoodsManagementDTO> goodsManagementDTOS = goodsManagementMapper.queryAllGoods(pageData.getParam());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (GoodsManagementDTO goodsManagementDTO : goodsManagementDTOS) {
            try {
                // 解析原始日期字符串为Date对象
                Date date = dateFormat.parse(goodsManagementDTO.getCreateTime());

                // 将Date对象格式化为yy-MM-dd格式的字符串
                String formattedDate = dateFormat.format(date);

                // 设置格式化后的日期字符串回Department对象
                goodsManagementDTO.setCreateTime(formattedDate);
            } catch (Exception e) {
                // 处理解析日期时可能抛出的异常
                e.printStackTrace();
            }
        }
        PageInfo pageInfo = new PageInfo(goodsManagementDTOS);
        return new ResponseData<>().ok(pageInfo);
    }


    @Override
    public ResponseData<?> addGoods(GoodsManagementDTO goodsManagementDTO) {
        Integer length = Integer.parseInt(goodsManagementDTO.getLength());
        Integer width = Integer.parseInt(goodsManagementDTO.getWidth());
        Integer height = Integer.parseInt(goodsManagementDTO.getHeight());
        Double volume = new Double(length) * new Double(width) * new Double(height);
        goodsManagementDTO.setVolume(volume);
        goodsManagementMapper.addGoods(goodsManagementDTO);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updateGoods(GoodsManagementDTO goodsManagementDTO) {
        Integer length = Integer.parseInt(goodsManagementDTO.getLength());
        Integer width = Integer.parseInt(goodsManagementDTO.getWidth());
        Integer height = Integer.parseInt(goodsManagementDTO.getHeight());
        Double volume = new Double(length) * new Double(width) * new Double(height);
        goodsManagementDTO.setVolume(volume);
        goodsManagementMapper.updateGoods(goodsManagementDTO);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> updateStatus(GoodsManagementDTO goodsManagementDTO) {
        if (goodsManagementDTO.getStatus().equals("0")){
            //上架 改成1
            goodsManagementDTO.setStatus("1");
            goodsManagementMapper.updateStatus(goodsManagementDTO);
            return new ResponseData<>().ok();
        }else if (goodsManagementDTO.getStatus().equals("1")){
            //下架  修改成0
            goodsManagementDTO.setStatus("0");
            goodsManagementMapper.updateStatus(goodsManagementDTO);
            return new ResponseData<>().ok();
        }

        return new ResponseData<>().fail(ResponseEnum.FAIL);
    }


    @Override
    public ResponseData<?> batchUpdateStatus1(List<GoodsManagementDTO> goodsManagementDTOS) {
        List<Integer> ids = new ArrayList<>();
        for (GoodsManagementDTO goodsManagementDTO : goodsManagementDTOS) {
            ids.add(goodsManagementDTO.getId());
        }
        goodsManagementMapper.batchUpdateStatus1(ids,"1");
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> batchUpdateStatus0(List<GoodsManagementDTO> goodsManagementDTOS) {
        List<Integer> ids = new ArrayList<>();
        for (GoodsManagementDTO goodsManagementDTO : goodsManagementDTOS) {
            ids.add(goodsManagementDTO.getId());
        }
        goodsManagementMapper.batchUpdateStatus1(ids,"0");
        return new ResponseData<>().ok();
    }
}
